﻿// Decompiled with JetBrains decompiler
// Type: Commom.Util.Conv
// Assembly: MT5Trade, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 8045A2CB-DBD6-41AD-8804-F98CAC1BDC8D
// Assembly location: C:\projects\trade\trade_tool\trade0812\MT5Trade.dll

using System;
using System.Reflection;
using System.Text;

#nullable enable
namespace Commom.Util;

public static class Conv
{
  public static int ToInt(object data)
  {
    if (data == null)
      return 0;
    int result;
    if (int.TryParse(data.ToString(), out result))
      return result;
    try
    {
      return Convert.ToInt32(Conv.ToDouble(data, 0));
    }
    catch (Exception)
    {
      return 0;
    }
  }

  public static int? ToIntOrNull(object data)
  {
    if (data == null)
      return new int?();
    int result;
    return int.TryParse(data.ToString(), out result) ? new int?(result) : new int?();
  }

  public static double ToDouble(object data)
  {
    double result;
    return data == null || !double.TryParse(data.ToString(), out result) ? 0.0 : result;
  }

  public static double ToDouble(object data, int digits) => Math.Round(Conv.ToDouble(data), digits);

  public static double? ToDoubleOrNull(object data)
  {
    if (data == null)
      return new double?();
    double result;
    return double.TryParse(data.ToString(), out result) ? new double?(result) : new double?();
  }

  public static Decimal ToDecimal(object data)
  {
    Decimal result;
    return data == null || !Decimal.TryParse(data.ToString(), out result) ? 0M : result;
  }

  public static Decimal ToDecimal(object data, int digits)
  {
    return Math.Round(Conv.ToDecimal(data), digits);
  }

  public static Decimal? ToDecimalOrNull(object data)
  {
    if (data == null)
      return new Decimal?();
    Decimal result;
    return Decimal.TryParse(data.ToString(), out result) ? new Decimal?(result) : new Decimal?();
  }

  public static Decimal? ToDecimalOrNull(object data, int digits)
  {
    Decimal? decimalOrNull = Conv.ToDecimalOrNull(data);
    return !decimalOrNull.HasValue ? new Decimal?() : new Decimal?(Math.Round(decimalOrNull.Value, digits));
  }

  public static Guid ToGuid(object data)
  {
    Guid result;
    return data == null || !Guid.TryParse(data.ToString(), out result) ? Guid.Empty : result;
  }

  public static Guid? ToGuidOrNull(object data)
  {
    if (data == null)
      return new Guid?();
    Guid result;
    return Guid.TryParse(data.ToString(), out result) ? new Guid?(result) : new Guid?();
  }

  public static DateTime ToDate(object data)
  {
    DateTime result;
    return data == null || !DateTime.TryParse(data.ToString(), out result) ? DateTime.MinValue : result;
  }

  public static DateTime? ToDateOrNull(object data)
  {
    if (data == null)
      return new DateTime?();
    DateTime result;
    return DateTime.TryParse(data.ToString(), out result) ? new DateTime?(result) : new DateTime?();
  }

  public static bool ToBool(object data)
  {
    if (data == null)
      return false;
    bool? nullable = Conv.GetBool(data);
    bool result;
    return nullable.HasValue ? nullable.Value : bool.TryParse(data.ToString(), out result) & result;
  }

  private static bool? GetBool(object data)
  {
    string lower = data.ToString().Trim().ToLower();
    if (lower != null)
    {
      switch (lower.Length)
      {
        case 1:
          switch (lower[0])
          {
            case '0':
              return new bool?(false);
            case '1':
              return new bool?(true);
            case '否':
              return new bool?(false);
            case '是':
              return new bool?(true);
          }
          break;
        case 2:
          if (lower == "no")
            return new bool?(false);
          break;
        case 3:
          if (lower == "yes")
            return new bool?(true);
          break;
        case 4:
          if (lower == "true")
            return new bool?(true);
          break;
        case 5:
          if (lower == "false")
            return new bool?(false);
          break;
      }
    }
    return new bool?();
  }

  public static bool? ToBoolOrNull(object data)
  {
    if (data == null)
      return new bool?();
    bool? nullable = Conv.GetBool(data);
    if (nullable.HasValue)
      return new bool?(nullable.Value);
    bool result;
    return bool.TryParse(data.ToString(), out result) ? new bool?(result) : new bool?();
  }

  public static string ToString(object data)
  {
    return data != null ? data.ToString().Trim() : string.Empty;
  }

  public static string ToDateString(DateTime time1, DateTime time2)
  {
    return $"{time1.ToyyyyMMdd()}-{time2.ToyyyyMMdd()}";
  }

  public static string JoinParamToString(object param)
  {
    string empty = string.Empty;
    StringBuilder data1 = new StringBuilder();
    data1.Append(empty);
    foreach (PropertyInfo property in param.GetType().GetProperties())
    {
      object data2 = property.GetValue(param, (object[]) null);
      if (data2 != null)
      {
        object str = (object) data2.ToStr();
        data1.AppendFormat("{0}={1}&", (object) property.Name, str);
      }
    }
    return data1.ToStr().Trim('&');
  }
}
